package com.pachong.spider;

import com.common.core.WyyMusicConstant;
import com.common.core.utils.WyyMusicUtils;
import com.domain.bean.music.PlaylistPool;
import com.domain.bean.music.SongMenu;
import com.pachong.service.MusicService;
import com.pachong.service.PlaylistPoolService;
import com.pachong.service.SongMenuService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.downloader.HttpClientDownloader;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.proxy.Proxy;
import us.codecraft.webmagic.proxy.SimpleProxyProvider;
import us.codecraft.webmagic.selector.Selectable;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:网易云音乐爬虫的页面处理
 * Author : wangyu
 * Date: 2018-03-05
 * Time: 16:06
 */
@Component
public class WyyMusicPageProcessor implements PageProcessor {
    private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    @Resource
    private SongMenuService songMenuService;
    @Resource
    private PlaylistPoolService playlistPoolService;

    @Override
    public void process(Page page) {

//        //爬去总歌单
//        List<String> discoverUrl = page.getHtml()
//                .xpath("//div[@id=\"cateListBox\"]")
//                .links()
//                .regex(WyyMusicConstant.DISCOVER_URL)
//                .all();
//        for (String url: discoverUrl) {
//            System.err.println("爬取到的discoverUrl:"+url);
//            PlaylistPool playlistPool = new PlaylistPool();
//            playlistPool.setCreateBy("-1");
//            playlistPool.setCreateDate(new Date());
//            playlistPool.setUrl(url);
//            playlistPoolService.save(playlistPool);
//        }


        //爬取歌单查询列表中的各种歌单分类的总歌单url
        List<String> playList = page.getHtml()
                .xpath("//ul[@id=\"m-pl-container\"]")
                .links()
                .regex(WyyMusicConstant.PLAYLIST_URL)
                .all();

        for (String url: playList) {
            System.err.println("爬取到的playListUrl:"+url);
            //根据歌单的CODE判断歌单是否已经保存到数据库
            String songMenuCode = url.split("id=")[1].trim();
            if(StringUtils.isNotBlank(songMenuCode)){
                SongMenu byCode = songMenuService.findByCode(songMenuCode);
                if(byCode == null){
                    SongMenu songMenu = new SongMenu();
                    songMenu.setUrl(url);
                    songMenu.setCode(url.split("id=")[1].trim());
                    try{
                        songMenuService.save(songMenu);
                    }catch (Exception e){
                        LOGGER.error(songMenuCode+" code:保存歌单信息异常:",e);
                    }

                }
            }
        }
//      爬取单个歌单中的分页歌单'下一页'信息
        List<String> all = page.getHtml()
                .xpath("//div[@id=\"m-pl-pager\"]")
                .xpath("//a[@class=\"zbtn znxt\"]")
                .links()
                .all();
        // 把下一页的URL加入队列继续爬虫
        page.addTargetRequests(all);
        for (String url: all) {
                System.err.println("爬取到的分页继续爬虫的url:"+url);
        }
    }

    @Override
    public Site getSite() {
        return WyyMusicUtils.createSite();
    }
    //爬虫歌单
    public void start() {
        long start = System.currentTimeMillis();

        PlaylistPool record = new PlaylistPool();
        record.setTimes(0);
        List<PlaylistPool> playlistPools = playlistPoolService.selectList(record);
//        ArrayList<String> strings = new ArrayList<>();
//        strings.add(WyyMusicConstant.START_DISCOVER_URL);
        Spider.create(this)
                .startUrls(
//                        strings
                        playlistPools.stream().map(playlistPool -> {
                    return playlistPool.getUrl();
                }).collect(Collectors.toList())
                )
                .setDownloader(WyyMusicUtils.getDownloader())
                .thread(WyyMusicConstant.THREAD_NUMS)
                .run();

        long end = System.currentTimeMillis();
        System.out.println("爬虫结束,耗时--->" + WyyMusicUtils.parseMillisecone(end - start));

    }

}
